/**
 *
 */
package com.apache.info.service.plugins;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.cache.util.Validator;
import com.apache.database.constant.SystemTools;
import com.apache.database.model.Page;
import com.apache.info.entity.PubMetadata;
import com.apache.info.entity.PubSortInfo;
import com.apache.info.manager.InfoReleaseManager;
import com.apache.info.manager.PubMetadataManager;
import com.apache.info.manager.PubSortInfoManager;
import com.apache.tools.ClassToolsUtil;
import com.apache.tools.StrUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * @author Administrator
 * 元素管理action插件
 */
public class PubSortInfoActionPluginImpl implements PluginConnector {
	protected Logger log = LoggerFactory.getLogger(getClass());
	private PubMetadataManager pubMetadataManager;

	private PubSortInfoManager pubSortInfoManager;

	private InfoReleaseManager InfoReleaseManager;

	/* (non-Javadoc)
	 * @see com.apache.api.manager.PluginConnector#execute(com.apache.api.vo.ParamsVo)
	 */
	public Object execute(ParamsVo param) throws Exception {
		ResultEntity entity = new ResultEntity();
		entity.setResult("true");
		String method = String.valueOf(param.getParams("method"));
		PubSortInfo item = new PubSortInfo();
		item = (PubSortInfo) ClassToolsUtil.getInstance().convert2Bean(param.getParams(), item);
		log.info("PubSortInfo Table operation ["+method+"]");
		if ("save".equals(method)) {
			saveMetadata(entity, item, param);
		} else if ("del".equals(method)) {
			delMetadata(entity, item, param);
		} else if ("list".equals(method)) {
			ParamsVo vo = new ParamsVo();
			int pageSize = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageSize")), "0"));
			int pageIndex = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageIndex")), "0"));
			vo.setObj(item);
			if (pageSize == 0 && pageIndex == 0) {
				List<PubSortInfo> list = pubSortInfoManager.getList(vo);
				entity.setEntity(list);
			} else {
				vo.setParams("pageIndex", pageIndex);
				vo.setParams("pageSize", pageSize);
				vo.setObj(item);
				Page pageList = pubSortInfoManager.getPageInfo(vo);
				entity.setEntity(pageList);
			}
			vo = null;
		} else if ("publish".equalsIgnoreCase(method)) {
			publish(entity, item, param);
		} else if ("stop".equalsIgnoreCase(method)) {
			stop(entity, item, param);
		} else if("createDatabase".equalsIgnoreCase(method)){
			param.setKey("createDatabase");
			try {
				InfoReleaseManager.execute(param);
				entity.setMessage("创建数据库成功");
				entity.setEntity("T");
			} catch (Exception e) {
				e.printStackTrace();
				entity.setMessage("创建数据库失败，"+e.getMessage());
				entity.setEntity("F");
			}
		}
		return entity;
	}

	private void stop(ResultEntity entity, PubSortInfo item, ParamsVo param) {
		if (Validator.isNotNull(item.getSortId())) {
			ParamsVo vo = new ParamsVo();
			vo.setInfoId(item.getSortId());
			PubSortInfo sort = (PubSortInfo) pubSortInfoManager.getInfoById(vo);
			if (Validator.isEmpty(sort)) {
				entity.setMessage("数据表信息不存在！");
				return;
			}
			sort.setSortStatus("0");
			vo.setObj(sort);
			vo.setKey("dropTable");
			vo.setParams("sort", sort);
			InfoReleaseManager.execute(vo);
			entity.setMessage("关闭成功");
			entity.setEntity("T");
			vo = null;
		} else {
			entity.setMessage("请不要恶意操作！");
		}
	}

	private void publish(ResultEntity entity, PubSortInfo item, ParamsVo param) {
		ParamsVo vo = new ParamsVo();
		if (Validator.isNotNull(item.getSortId())) {
			vo.setInfoId(item.getSortId());
			PubSortInfo sort = (PubSortInfo) pubSortInfoManager.getInfoById(vo);
			if (Validator.isEmpty(sort)) {
				entity.setMessage("数据表信息不存在！");
				return;
			}
			ParamsVo<PubMetadata> met = new ParamsVo<PubMetadata>();
			PubMetadata pm = new PubMetadata();
			pm.setSortId(sort.getSortId());
			met.setObj(pm);
			long count = pubMetadataManager.countInfo(met);
			if (count <= 0) {
				entity.setMessage("数据表下不存在相应元素,不能启用此数据表！");
				return;
			}
			if (Validator.isNull(sort.getInfoTableName())) {
				sort.setInfoTableName(sort.getTablePrefix() + "_" + sort.getSortCode());
			}
			vo.setParams("sort", sort);
			vo.setParams("tableName", sort.getInfoTableName());
			vo.setKey("exisTable");
			count = Long.valueOf(String.valueOf(InfoReleaseManager.execute(vo)));
			if (count > 0) {
				sort.setSortStatus("1");
				vo.setObj(sort);
				boolean mark = pubSortInfoManager.editInfo(vo);
				entity.setEntity(mark);
				entity.setMessage("数据表已存在不能再次启用！");
				return;
			} else {
				vo.setKey("createTable");
				vo.setParams("sort", sort);
				InfoReleaseManager.execute(vo);
				entity.setMessage("启用成功");
				entity.setEntity("T");
			}
		} else {
			entity.setMessage("请不要恶意操作！");
		}
	}

	private void delMetadata(ResultEntity entity, PubSortInfo item, ParamsVo param) {
		ParamsVo vo = new ParamsVo();
		if (Validator.isNotNull(item.getSortId())) {
			vo.setInfoId(item.getSortId());
			PubSortInfo sort = (PubSortInfo) pubSortInfoManager.getInfoById(vo);
			if (Validator.isEmpty(sort)) {
				entity.setMessage("删除的数据表不存在！");
				return;
			}
			if ("1".equals(sort.getSortStatus())) {
				entity.setMessage("数据表已被启用,不能做删除操作！");
				return;
			} else {
				ParamsVo<PubMetadata> met = new ParamsVo<PubMetadata>();
				PubMetadata pm = new PubMetadata();
				pm.setSortId(sort.getSortId());
				met.setObj(pm);
				long count = pubMetadataManager.countInfo(met);
				if (count > 0) {
					entity.setMessage("数据表下存在字段,请先删除其下字段！");
					return;
				}
			}
			boolean mark = pubSortInfoManager.deleteInfo(vo);
			if (!mark) {
				entity.setMessage("删除失败！");
			} else {
				entity.setMessage("数据表信息删除成功");
				entity.setEntity("T");
			}
		} else {
			entity.setMessage("请不要恶意操作！");
		}
		vo = null;
	}

	private void saveMetadata(ResultEntity entity, PubSortInfo item, ParamsVo param) {
		if (Validator.isNull(item.getSysName())) {
			entity.setMessage("所属系统不能为空");
			return;
		}
		ParamsVo vo = new ParamsVo();
		if (Validator.isNull(item.getSortId())) {
			vo.setInfoId(item.getSortCode());
			vo.setMethodKey("BySortCode");
			vo.setParams("sysName", item.getSysName());
			PubSortInfo obj = (PubSortInfo) pubSortInfoManager.getInfoById(vo);
			vo.setMethodKey("");
			if (!Validator.isEmpty(obj)) {
				entity.setMessage("数据表[" + item.getSortCode() + "]已存在,请选择其他的名字");
				return;
			}
			vo.setParams("mainTab", String.valueOf(param.getParams("mainTab")));
			if (Validator.isNull(item.getInfoTableName())) {
				//设置表单前缀 INFO_TABLE_NAME配置
				String tableNameQian = "Pub_";
				try {
					String qianz = SystemTools.getInstance().getValue("INFO_TABLE_NAME");
					if (StrUtil.isNotNull(qianz)) {
						if (qianz.indexOf("_") < 0) {
							qianz += "_";
						}
						tableNameQian = qianz;
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				String tableName = tableNameQian + item.getSortCode();
				item.setInfoTableName(tableName.toUpperCase());
			}
			item.setSortStatus("0");
			vo.setObj(item);
			String id = pubSortInfoManager.saveInfo(vo);
			entity.setMessage("数据表信息添加成功");
			entity.setEntity(id);
		} else {
			vo = new ParamsVo();
			vo.setInfoId(item.getSortId());
			PubSortInfo objs = (PubSortInfo) pubSortInfoManager.getInfoById(vo);
			if ("1".equals(objs.getSortStatus())) {
				entity.setMessage("表已启用后不能对表名进行修改");
			} else {
				if (!Validator.isEmpty(objs) && !item.getSortId().equals(objs.getSortId())
						&& objs.getSysName().equals(item.getSysName())) {
					entity.setMessage("分类[" + item.getSortCode() + "]已存在,请选择其他的名字");
					return;
				}
				vo.setObj(item);
				boolean mark = pubSortInfoManager.editInfo(vo);
				if (!mark) {
					entity.setMessage("数据表信息修改失败！");
				} else {
					entity.setMessage("修改成功");
					entity.setEntity(item.getSortId());
				}
			}
		}
		vo = null;
	}

	public void setPubMetadataManager(PubMetadataManager pubMetadataManager) {
		this.pubMetadataManager = pubMetadataManager;
	}

	public void setPubSortInfoManager(PubSortInfoManager pubSortInfoManager) {
		this.pubSortInfoManager = pubSortInfoManager;
	}

	public void setInfoReleaseManager(InfoReleaseManager infoReleaseManager) {
		InfoReleaseManager = infoReleaseManager;
	}

}
